<?php
class User {
    
    /**
     * Login
     * 
     * @param  $strUserName
     * @param  $strPassword
     * @return void
     * @author andy
     */
    public static function login( $strUserName, $strPassword ) {
        
        if ( ! empty($strUserName) && ! empty($strPassword) ) {
            
            $obj_model_user = D('SysUser');
            $arr_user_info  = $obj_model_user->get_info_by_user_name($strUserName, $strPassword);

            if ( ! empty($arr_user_info) ) {
                
                $int_user_id = $arr_user_info['user_id'];
                
                // Update
                $obj_model_user->update_info($int_user_id);
                
                // Acl
                $arr_role_info = array();
                
                if ( ! $arr_user_info['is_admin'] ) {
                    
                    // Group info
                    $arr_group_info = D('SysGroup')->get_all(array('status'=>1, 'group_id'=>array('IN', $arr_user_info['group_ids'])));
                    $str_role_ids   = '';
                    
                    foreach ( $arr_group_info as $sub ) {
                        $str_role_ids .= ! empty($sub['role_ids']) ? $sub['role_ids'] . ',' : ''; 
                    }
                    
                    // Role info
                    $arr_role_info = D('SysRole')->get_all(array('status'=>1, 'role_id'=>array('IN', $str_role_ids)));
    
                } else {// User
                
                    $arr_role_info = D('SysRole')->get_all();
                }
                
                foreach ( $arr_role_info as $sub ) {
                    // Menu info
                    $arr_menu_info[$sub['group_name']][$sub['module_name']][$sub['menu_name']][$sub['action_name']] = $sub;
                    
                    // Role info
                    if ( $sub['level'] > 2 ) {
                        $arr_user_role_info[] = $sub['url'];
                    }
                }
                
                // 默认权限
                $arr_user_role_info[] = '/index/index';

                // Menu html
                $str_menu_html = self::get_menu_tree_html($arr_menu_info);
                
                // Session
                session('user_info', array(
                                           'user_id'    => $int_user_id, 
                                           'user_name'  => $arr_user_info['user_name'],
                                           'is_admin'   => $arr_user_info['is_admin'],
                                           'role_info'  => $arr_user_role_info,
                                           'menu_html'  => $str_menu_html
                                     ));
                                 
                log_db(__METHOD__, $int_user_id, '用户登录', $strUserName);
                
                redirect('/');
            }
        }
        
        redirect('/login');
    }
    
    /**
     * Logout
     * 
     * @param  void
     * @return void
     * @author andy
     */
    public static function logout() {
        
        // Session
        session('user_info', null);
        
        redirect('/login');
    }
    
    /**
     * Check login
     * 
     * @param  void
     * @return bool
     * @author andy
     */
    public static function check_login() {
        
        return isset($_SESSION['user_info']);
    }
    
    /**
     * Check role
     * 
     * @param  void
     * @return bool
     * @author andy
     */
    public static function check_role() {

        return $_SESSION['user_info']['is_admin'] ? true : in_array(__ACTION__, $_SESSION['user_info']['role_info']);
    }
    
    /**
     * Get login user info
     * 
     * @param  $strKey
     * @return array
     * @author andy
     */
    public static function get_login_user_info( $strKey = '' ) {
        
        return $strKey ? $_SESSION['user_info'][$strKey] : $_SESSION['user_info'];
    }
    
    /**
     * Get login user name
     * 
     * @param  void
     * @return string
     * @author andy
     */
    public static function get_login_user_name() {
        
        return $_SESSION['user_info']['user_name'];
    }
    
    /**
     * Get login user id
     * 
     * @param  void
     * @return string
     * @author andy
     */
    public static function get_login_user_id() {
        
        return $_SESSION['user_info']['user_id'];
    }
    
    /**
     * Get user info
     * 
     * @param  $intUserId
     * @param  $strKey
     * @return array
     * @author andy
     */
    public static function get_user_info( $intUserId, $strKey = '' ) {
        
        $arr_info = D('SysUser')->get_info($intUserId);
        
        return $strKey ? $arr_info[$strKey] : $arr_info;
    }
    
    /**
     * Get user name
     * 
     * @param  $intUserId
     * @return string
     * @author andy
     */
    public static function get_user_name( $intUserId ) {
        
        return self::get_user_info($intUserId, 'user_name');
    }
    
    /**
     * Get role info
     * 
     * @param  void
     * @return array
     * @author andy
     */
    public static function get_role_info() {
        
        $arr_info_result = D('SysRole')->get_all();
        
        foreach ( $arr_info_result as $sub ) {
            
            $arr_info[$sub['group_name']][$sub['module_name']][$sub['menu_name']][$sub['action_name']] = $sub;
        }
       
        return $arr_info;
    }
    
    /**
     * Get role info, tree html
     * 
     * @param  void
     * @return array
     * @author andy
     */
    public static function get_role_tree_html( $arrRoleIds = array() ) {
        
        $arr_info  = self::get_role_info();
        $str_html  = '<ul class="tree treeFolder expand">';
        $bln_check = ! empty($arrRoleIds) ? true : false; 
        
        foreach ( $arr_info as $key=>$sub ) {

            // Group
            $str_html .= '<li><label><input type="checkbox" name="role_ids[]" value="' . $sub['']['']['']['role_id'] . '" ' . ($bln_check ? (in_array($sub['']['']['']['role_id'], $arrRoleIds) ? 'checked' : '') : '') . ' title="' . $sub['']['']['']['url'] . '" />' . $sub['']['']['']['role_name'] . '</label>';
            
            // Module
            $str_html .= '<ul>';
            foreach ( $sub as $sub2 ) {
                
                if ( ! empty($sub2) && $sub2['']['']['level'] == 2 ) {
                    $str_html .= '<li><label><input type="checkbox" name="role_ids[]" value="' . $sub2['']['']['role_id'] . '" ' . ($bln_check ? (in_array($sub2['']['']['role_id'], $arrRoleIds) ? 'checked' : '') : '') . ' title="' . $sub2['']['']['url'] . '" />' . $sub2['']['']['role_name'] . '</label>';

                    // Menu
                    $str_html .= '<ul>';
                    foreach ( $sub2 as $sub3 ) {

                        if ( ! empty($sub3) && $sub3['']['level'] == 3 ) {
                            $str_html .= '<li><label><input type="checkbox" name="role_ids[]" value="' . $sub3['']['role_id'] . '" ' . ($bln_check ? (in_array($sub3['']['role_id'], $arrRoleIds) ? 'checked' : '') : '') . ' title="' . $sub3['']['url'] . '" />' . $sub3['']['role_name'] . '</label>';
                            
                            // Action
                            $str_html .= '<ul>';
                            foreach ( $sub3 as $sub4 ) {
                                
                                if ( ! empty($sub4) && $sub4['level'] == 4 ) {
                                    $str_html .= '<li><label><input type="checkbox" name="role_ids[]" value="' . $sub4['role_id'] . '" ' . ($bln_check ? (in_array($sub4['role_id'], $arrRoleIds) ? 'checked' : '') : '') . ' title="' . $sub4['url'] . '" />' . $sub4['role_name'] . '</label>';
                                }
                            }
                            $str_html .= '</ul>';
                        }
                    }
                    $str_html .= '</ul></li>';
                }
            }
            $str_html .= '</ul></li>';
        }
        
        $str_html .= '</ul>';
        
        return $str_html;
    }
    
    /**
     * Get menu info, tree html
     * 
     * @param  $arrMenuInfo
     * @return array
     * @author andy
     */
    public static function get_menu_tree_html( $arrMenuInfo ) {
        
        $str_html  = '';

        foreach ( $arrMenuInfo as $key=>$sub ) {

            // Menu 1
            $str_html .= '<div class="accordionHeader"><h2 group_name="' . $sub['']['']['']['group_name'] . '"><span>Folder</span>' . $sub['']['']['']['role_name'] . '</h2></div>';
            
            // Menu 2
            $str_html .= '<div class="accordionContent"><ul class="tree treeFolder">';
            foreach ( $sub as $sub2 ) {
                
                if ( ! empty($sub2) && $sub2['']['']['level'] == 2 ) {
                    
                    // Menu 2
                    $str_html .= '<li><a href="javascript:void(0);" module_name="' . $sub2['']['']['module_name'] . '">' . $sub2['']['']['role_name'] . '</a>';

                    // Menu 3
                    $str_html .= '<ul>';
                    foreach ( $sub2 as $sub3 ) {

                        if ( ! empty($sub3) && $sub3['']['level'] == 3 ) {
                            
                            // Menu 3
                            $str_html .= '<li><a href="' . $sub3['']['url'] . '" target="navTab" rel="' . str_replace('/', '_', $sub3['']['url']) . '" menu_name="' . $sub3['']['menu_name'] . '">' . $sub3['']['role_name'] . '</a></li>';
                        }
                    }
                    $str_html .= '</ul></li>';
                }
            }
            $str_html .= '</ul></div>';
        }
        
        return $str_html;
    }
}